package com.example.demo.controller;

import com.example.demo.DTO.HR_Login_DTO;

import com.example.demo.Server.HRServer;
import com.example.demo.entity.HR;
import com.example.demo.entity.JobPosition;
import com.example.demo.entity.User;
import com.example.demo.result.Result;
import com.example.demo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class HR_Controller {



    @Autowired
    private HRServer hr_server;
    @PostMapping("/HR_login")
    public Result<HR> HR_login(@RequestBody HR_Login_DTO hr_login_dto)
    {
        HR login = hr_server.login(hr_login_dto);


        //登入成功生成并下发令牌
        if(login!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",login.getHrId());

            String jwt = JwtUtils.generateJwt(claims);
            login.setToken(jwt);
            return Result.success(login);
        }
        //登入失败返回错误信息
        if(login != null)
        {
            return Result.success(login);
        }

        return Result.error("用户名或密码错误");
    }
    @PostMapping("/HR_Register")
    public Result HR_Register(@RequestBody HR hr)
    {
        int register = hr_server.Register(hr);
        if(register == 1)
        {
            return Result.success(register);
        }
        return Result.error("注册失败");
    }
    @PostMapping("/HR_updata")
    public Result HR_updata(@RequestBody HR hr)
    {
        int updata = hr_server.updata(hr);
        if(updata == 1)
        {
            return Result.success(updata);
        }
        return Result.error("更新失败");



    }

    @GetMapping("/HR_select")
    public Result<ArrayList<JobPosition>> HR_select(Integer id)
    {
        ArrayList<JobPosition> select = hr_server.select(id);
        if(select != null)
        {
            return Result.success(select);
        }
        return Result.error("查询失败");
    }


}
